import 'package:flutter/material.dart';

class NavigationDemo extends StatefulWidget {
  @override
  _NavigationState createState() => _NavigationState();
}

class _NavigationState extends State<NavigationDemo> {
  int _selectedIndex = 0; //当前选中项的索引
  final _widgetOptions = <Widget>[
    Text('index 0：信息'),
    Text('index 1：通讯录'),
    Text('index 2：发现'),
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      // 导航栏
      appBar: AppBar(
        title: Text('BottomNavigationBar示例'),
        centerTitle: true,
        // 导航栏右侧菜单
        actions: <Widget>[
          IconButton(
            icon: Icon(Icons.share),
            onPressed: () {},
          )
        ],
      ),
      body: Center(
        child: _widgetOptions[_selectedIndex],
      ),
      // 底部导航
      bottomNavigationBar: BottomNavigationBar(
        currentIndex: _selectedIndex, //当前选中项的索引
        fixedColor: Theme.of(context).accentColor, //选项中项的颜色
        onTap: (index) {
          setState(
            () {
              _selectedIndex = index;
            },
          );
        },
        items: <BottomNavigationBarItem>[
          BottomNavigationBarItem(icon: Icon(Icons.message), label: '信息'),
          BottomNavigationBarItem(
              icon: Icon(Icons.perm_contact_calendar), label: '通讯录'),
          BottomNavigationBarItem(
              icon: Icon(Icons.account_circle), label: '发现'),
        ],
      ),
      // 悬浮按钮
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.add),
        onPressed: () {},
      ),
    );
  }
}
